package Demo;
class Try{
    public synchronized void method(){
        System.out.println("这是一个线程");
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Try t = new Try();
        t.method();
    }
    public static void main3(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()-> {
            System.out.println("Hello thread1");
        });
        Thread thread2 = new Thread(()-> {
            System.out.println("Hello thread2");
        });
        thread1.start();//启动thread1线程
        thread2.start();//启动thread2线程
        System.out.println(thread1.getState());//获取thread1线程的状态
        System.out.println(thread2.getState());//获取thread2线程的状态
    }
    public static void main2(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                System.out.print(i+" ");
            }
        });
        thread.start();
        thread.join();
        System.out.println("ok");
    }
    public static void main1(String[] args) throws InterruptedException {
        Thread thread = new Thread(()-> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("Hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        thread.start();//启动thread线程

        Thread.sleep(3000);//main线程里面的sleep方法

        thread.interrupt();//interrupt把标志位设置为true
    }
}
